#include "rrobject.h"

RRObject::RRObject()
{
    map_index = 0;
}

void RRObject::rrBeginComponent(RRMaterial *arg)
{
    compset[map_index] = new RRComponent;
    compset[map_index]->rrSetMaterial(arg);
}

void RRObject::rrSetPrim(RRPrim *arg)
{
    compset[map_index]->rrSetPrim(arg);
}

void RRObject::rrEndComponent()
{
    map_index++;
}

void RRObject::rrTrace(RRRay *ray, RRCrossInfo *arg)
{
    RRComponent *cmp;
    RRCrossInfo cr, cr_temp;
    cr.rrSetHit(false);
    RRRange rng, rng_temp;
    rng.s = 1.0e+18;
    map<int, RRComponent*>::iterator it = compset.begin();
    while(it != compset.end()) {
        cmp = (*it).second;
        cmp->rrTrace(ray, &cr_temp);
        if(cr_temp.rrGetHit() == true) {
            //puts("why dont come");
            rng_temp = cr_temp.rrGetRange();
            if(rng_temp.s < rng.s) {
                rng.s = rng_temp.s;
                cr.rrSetCrossInfo(&cr_temp);
            }
        }
        it++;
    }
    arg->rrSetCrossInfo(&cr);
}
